﻿using System;
using System.Reflection;
using Autofac;
using Phoenix.Framework;

// ReSharper disable CheckNamespace
namespace Phoenix.Extensions.Autofac
{
    public static class AutofacExtensions
    {
        public static void RegisterControllers(
            this Assembly assembly, 
            ContainerBuilder containerBuilder, 
            IControllerLocator controllerLocator = null)
        {
            if (controllerLocator == null)
                controllerLocator = new DefaultControllerLocator();

            var controllers = controllerLocator.GetControllersInAssembly(assembly);
            foreach (var controller in controllers)
            {
                containerBuilder
                    .RegisterType(controller)
                    .AsSelf();
            }
        }

        /// <summary>
        /// Registers view models as themselves and as implemented interfaces
        /// </summary>
        /// <param name="assembly"></param>
        /// <param name="containerBuilder"></param>
        public static void RegisterViewModels(
            this Assembly assembly,
            ContainerBuilder containerBuilder)
        {
            containerBuilder
                .RegisterAssemblyTypes(assembly)
                .AssignableTo<ViewModelBase>()
                .AsImplementedInterfaces()
                .AsSelf();
        }
    }
}
// ReSharper restore CheckNamespace
